<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>表格</title>
    <link rel="stylesheet" href="../frame/layui/css/layui.css">
    <link rel="stylesheet" href="../frame/static/css/style.css">
    <link rel="icon" href="../frame/static/image/code.png">
</head>
<body class="body">
<div class="layui-container">
    <blockquote class="layui-elem-quote">添加管理员</blockquote>
    <form class="layui-form layui-form-pane ml5" onsubmit="return false;">
        <div class="layui-form-item">
            <label class="layui-form-label">用户名</label>
            <div class="layui-input-inline">
                <input type="text" name="name" required lay-verify="required" lay-verType="tips"
                       placeholder="请输入用户名"
                       autocomplete="off" class="layui-input" value="">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">密码框</label>
            <div class="layui-input-inline">
                <input type="password" name="password" placeholder="请输入密码"
                       autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">确认密码</label>
            <div class="layui-input-inline">
                <input type="password" name="password2" placeholder="请输入密码"
                       autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <input type="button" class="layui-btn" lay-submit lay-filter="success" value="立即提交"/>
                <a href="javascript:window.history.back()" class="layui-btn layui-btn-primary">返回</a>
                <input type="hidden" name="id" value="0">
            </div>
        </div>
    </form>
</div>
<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
<script type="text/javascript" src="../frame/layui/layui.js"></script>
<script>
    /**
     * 获取url中 参数
     * @param {String} name - 参数名
     * @returns {*}
     * @constructor
     */
    function getQueryString(name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
        var r = window.location.search.substr(1).match(reg);
        if (r != null) return decodeURI(r[2]);
        return null;
    }

    $(function () {
        var id = getQueryString('id');
        console.log(id);
        id = parseInt(id);
        if (id > 0) {
            $.post("../../../action/sys.php?act=user_edit", {id: id}, function (res) {
                if (res.status === 0) {
                    $('input[name="id"]').val(res.data.id);
                    $('input[name="name"]').val(res.data.name);
                }
            }, 'json');
        }
    });
    layui.use(['table', 'form', 'element'], function () {
        var form = layui.form;

        //监听提交
        form.on('submit(success)', function (data) {
            console.log(JSON.stringify(data.field));
            $.post("../../../action/sys.php?act=reg", data.field, function (res) {
                if (res.status === 0) {
//                    window.location.href = res.data;
                    history.back();
                }
                else {
                    layer.msg(res.msg);
                }
            }, 'json');
            return false;
        });
    });
</script>
</body>
</html>